//
//  MCWelcomeViewController.swift
//  HXWeibo
//
//  Created by HX on 15/11/26.
//  Copyright © 2015年 HX. All rights reserved.
//

import UIKit
import SDWebImage

class MCWelcomeViewController: UIViewController
{
    // MARK: - iconView底部约束
    private var iconViewBottomCon: NSLayoutConstraint?
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        prepareUI()
        
        // 显示用户头像
        if let avatar_large = MCUserAccount.loadUserAccount()?.avatar_large
        {
            iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        // 获取用户数据
        MCUserAccount.loadUserAccount()?.loadUserInfo()
    }
    
    // 在用户看得到view的时候开始做动画
    override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }
    
    // MARK: - 头像动画
    private func moveAnimation()
    {
        //改变头像的位置
        iconViewBottomCon?.constant = -500
        
        // duration: 1.2
        // delay: 延时
        // usingSpringWithDamping: 弹簧效果明显程度 0 - 1
        // initialSpringVelocity: 初始速度
        // 弹簧效果动画
        UIView.animateWithDuration(1.2, delay: 0.2, usingSpringWithDamping: 0.45, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations:
            { () -> Void in
                // 在动画中更新布局
                self.view.layoutIfNeeded()
                
            })
            { (_) -> Void in
                print("动画结束")
                
                // 切换到MainViewController界面
                AppDelegate.switchRootViewController(true)
        }
    }
    
    // MARK: - 准备UI
    private func prepareUI()
    {
        view.addSubview(bjView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)

        // 设置约束
        bjView.ff_Fill(view)
        
        // 头像 
        let constraint = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        
        // 获取view上面的约束,哪个view要获取约束，就用哪个view调用
        iconViewBottomCon = iconView.ff_Constraint(constraint, attribute: NSLayoutAttribute.Bottom)
        
        //label
        messageLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
    }
    
    // MARK: - 懒加载控件
    /// 背景
    private lazy var bjView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像
    private lazy var iconView: UIImageView =
    {
        // 头像的占位图片
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 设置圆角
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true;
        
        return iconView
    }()
    
    /// 消息 label
    private lazy var messageLabel: UILabel =
    {
        let label = UILabel()
        
        label.text = "欢迎回来"
        label.sizeToFit()
        
        return label
    }()
}


































